package com.maxd.controller.activity;

import com.maxd.adapter.IgnoreSecurity;
import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.bean.activity.ActivityJoinData;
import com.maxd.bean.activity.JoinActivityReq;
import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.ActivityJoinRecord;
import com.maxd.model.RwActivity;
import com.maxd.service.activity.IRwActivityService;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.AssertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@Api(value="戎威少年活动管理", tags = {"戎威少年活动管理"})
@RequestMapping(value = "/rw-activity")
public class RwActivityController {

    private static final Logger log = LoggerFactory.getLogger(RwActivityController.class);
    @Autowired
    private IRwActivityService rwActivityService;

    @PostMapping("/save-activity")
    @ApiOperation("新增/修改活动")
    public Result<Void> save(@RequestBody @Validated RwActivity rwActivity) {
        AdminUtils.checkLogin();
        rwActivityService.save(rwActivity);
        return ResultUtil.success();
    }

    @IgnoreSecurity
    @GetMapping("/activity-list")
    @ApiOperation("查询活动列表，按创建时间倒序排序")
    public Result<PageRes<RwActivity>> list(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                            @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                            @ApiParam("活动名称，支持模糊查询") @RequestParam(value = "name", required = false) String name,
                                            @ApiParam("活动类型，SQ-社区活动 HW-户外活动") @RequestParam(value = "activityType", required = false) String activityType,
                                            @ApiParam("活动结束时间最小值，格式为yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTimeMin", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTimeMin) {
        log.info("endTimeMin = {}", endTimeMin);
        PageRes<RwActivity> pageRes = rwActivityService.list(pageNo, pageSize, name, activityType, endTimeMin);
        return ResultUtil.success(pageRes);
    }

    @IgnoreSecurity
    @GetMapping("/activity-detail")
    @ApiOperation("查询活动详情")
    public Result<RwActivity> detail(@ApiParam("活动ID") @RequestParam("id") Long id) {
        RwActivity detail = rwActivityService.detail(id);
        return ResultUtil.success(detail);
    }

    @PostMapping("/join-activity")
    @ApiOperation("参加活动")
    public Result<ActivityJoinRecord> join(@RequestBody @Validated JoinActivityReq req) {
        ActivityJoinRecord record = rwActivityService.join(req);
        return ResultUtil.success(record);
    }

    @GetMapping("/activity-join-list")
    @ApiOperation("内管查询活动参与记录")
    public Result<PageRes<ActivityJoinData>> join(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                  @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                  @ApiParam("活动ID") @RequestParam(value = "activityId", required = false) Long activityId,
                                                  @ApiParam("用户ID") @RequestParam(value = "userId", required = false) Long userId,
                                                  @ApiParam("状态 0-初始化 1-付款中 2-完成 3-取消") @RequestParam(value = "status", required = false) String status) {
        AdminUtils.checkLogin();
        PageRes<ActivityJoinData> pageRes = rwActivityService.joinList(pageNo, pageSize, userId, activityId, status);
        return ResultUtil.success(pageRes);
    }

    @GetMapping("/my-join-list")
    @ApiOperation("商城查询本人活动参与记录，")
    public Result<PageRes<ActivityJoinData>> join(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                  @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                  @ApiParam("状态 0-初始化 1-付款中 2-完成 3-取消") @RequestParam(value = "status", required = false) String status) {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        PageRes<ActivityJoinData> pageRes = rwActivityService.joinList(pageNo, pageSize, userId, null, status);
        return ResultUtil.success(pageRes);
    }

}